package com.example.springbootkafka.ext;

import com.example.springbootkafka.bean.User;
import org.apache.kafka.common.header.Headers;
import org.apache.kafka.common.serialization.Deserializer;

import java.nio.ByteBuffer;
import java.util.Map;

/**
 * @ClassName UserDeserializable
 * @Description TODO
 * @Auth ying.xiao
 * @Date 2021/7/14
 * @Version 1.0.0
 */
public class UserDeserializable implements Deserializer<User> {

    @Override
    public void configure(Map<String, ?> configs, boolean isKey) {

    }

    @Override
    public User deserialize(String s, byte[] data) {
        ByteBuffer allocate = ByteBuffer.allocate(data.length);
        allocate.put(data);
        allocate.flip();
        int userId = allocate.getInt();
        int length = allocate.getInt();
        System.out.println(length);
        String username = new String(data, 8, length);
        return new User(userId, username);
    }

    @Override
    public User deserialize(String topic, Headers headers, byte[] data) {
//        ByteBuffer allocate = ByteBuffer.allocate(data.length);
//        allocate.put(data);
//        allocate.flip();
//        int userId = allocate.getInt();
//        int length = allocate.getInt();
//        System.out.println(length);
//        String username = new String(data, 8, length);
        return new User(123, "xiaoying");
    }

    @Override
    public void close() {

    }
}
